package com.sky.service.admin.impl;

import com.sky.mapper.admin.DishMapper;
import com.sky.mapper.admin.SetMealMapper;
import com.sky.mapper.user.OrderMapper;
import com.sky.mapper.user.UserMapper;
import com.sky.pojo.Orders;
import com.sky.service.admin.WorkspaceService;
import com.sky.vo.BusinessDataVO;
import com.sky.vo.DishOverViewVO;
import com.sky.vo.OrderOverViewVO;
import com.sky.vo.SetmealOverViewVO;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDate;

@Service
public class WorkspaceServiceImpl implements WorkspaceService {
    @Resource
    private OrderMapper orderMapper;
    @Resource
    private UserMapper userMapper;
    @Resource
    private DishMapper dishMapper;
    @Resource
    private SetMealMapper setMealMapper;

    /**
     * 今日数据的接口设计
     **/
    @Override
    public BusinessDataVO getBusinessDate(LocalDate begin, LocalDate end) {
        BusinessDataVO businessDataVO = new BusinessDataVO();
        //获得当天营业额
        Double turnover = orderMapper.selectSalesByDay(begin.toString());
        turnover = turnover == null ? 0.0 : turnover;
        businessDataVO.setTurnover(turnover);
        //当天的所有订单数
        int allTotal = orderMapper.selectOrdersByDateAndStatus(begin.toString(), end.toString(), null);
        //获得当天有效订单数
        int validOrderCount = orderMapper.selectOrdersByDateAndStatus(begin.toString(), end.toString(), Orders.COMPLETE_ORDER);
        businessDataVO.setValidOrderCount(validOrderCount);
        //获得当天订单完成率
        if (allTotal == 0) {
            businessDataVO.setOrderCompletionRate(0.0);
        } else {
            double orderCompletionRate = validOrderCount * 1.0 / allTotal;
            businessDataVO.setOrderCompletionRate(orderCompletionRate);
        }
        //获得当天平均客单价
        if (validOrderCount == 0) {
            businessDataVO.setUnitPrice(0.0);
        } else {
            Double unitPrice = turnover * 1.0 / validOrderCount;
            businessDataVO.setUnitPrice(unitPrice);
        }
        //获得当天新增用户数
        Integer newUser = userMapper.countNewUserByDate(begin.toString());
        businessDataVO.setNewUsers(newUser);
        return businessDataVO;
    }

    /**
     * 查询订单管理数据
     */
    @Override
    public OrderOverViewVO overviewOrder() {
        //获得当天的开始时间
        LocalDate begin = LocalDate.now();
        //获得当天的结束时间
        LocalDate end = LocalDate.now();
        //待接单
        Integer waitingOrders = orderMapper.selectOrdersByDateAndStatus(begin.toString(), end.toString(), Orders.TO_BE_CONFIRMED);
        //待派送
        Integer deliveredOrders = orderMapper.selectOrdersByDateAndStatus(begin.toString(), end.toString(), Orders.CONFIRMED);
        //已完成
        Integer completedOrders = orderMapper.selectOrdersByDateAndStatus(begin.toString(), end.toString(), Orders.COMPLETE_ORDER);
        //已取消
        Integer cancelledOrders = orderMapper.selectOrdersByDateAndStatus(begin.toString(), end.toString(), Orders.CANCELLED_ORDER);
        //全部订单
        Integer allOrders = orderMapper.selectOrdersByDateAndStatus(begin.toString(), end.toString(), null);
        return OrderOverViewVO.builder()
                .waitingOrders(waitingOrders)
                .deliveredOrders(deliveredOrders)
                .completedOrders(completedOrders)
                .cancelledOrders(cancelledOrders)
                .allOrders(allOrders)
                .build();
    }

    /**
     * 菜品总览的接口设计
     */
    @Override
    public DishOverViewVO overviewDishes() {
        // 已启售数量
        Integer sold = dishMapper.countStatus(Orders.PAID);
        // 已停售数量
        Integer discontinued = dishMapper.countStatus(Orders.UN_PAID);
        return new DishOverViewVO(sold, discontinued);
    }

    /**
     * 查询套餐总览
     */
    @Override
    public SetmealOverViewVO setmealOverView() {
        // 已启售数量
        Integer sold = setMealMapper.countStatus(Orders.PAID);
        // 已停售数量
        Integer discontinued = setMealMapper.countStatus(Orders.UN_PAID);
        return new SetmealOverViewVO(sold, discontinued);
    }

    public BusinessDataVO BusinessDate(LocalDate begin, LocalDate end){
        BusinessDataVO businessDataVO = new BusinessDataVO();
        Double turnover = orderMapper.BusinessDate(begin.toString(),end.toString());
        turnover = turnover == null ? 0.0 : turnover;
        businessDataVO.setTurnover(turnover);
        Integer newUser = userMapper.countNewUser(begin.toString(),end.toString());
        businessDataVO.setNewUsers(newUser);
        return businessDataVO;
    }
}
